/*
 * BncsCheckClient.h
 *
 *  Created on: Nov 20, 2008
 *      Author: nathan
 */

#ifndef BNCSAUTHCHECKCLIENT_H_
#define BNCSAUTHCHECKCLIENT_H_

#include "BncsMessage.h"
#include "BncsMessageFactory.h"

struct BncsKeyInfo
{
	quint32 length;
	quint32 product;
	quint32 publicValue;
	quint32 unknown;
	quint8 hashData[20];
};

class BncsAuthCheckClientData : public QSharedData
{
public:
	quint32 clientToken;
	quint32 exeVersion;
	quint32 exeHash;
	quint32 numKeys;
	quint32 bSpawn;
	BncsKeyInfo keyInfo[2];
	QByteArray exeInfo;
	QByteArray keyOwner;
};

class BncsAuthCheckClient : public BncsMessage
{
	BNCS_MESSAGE_REGISTER(BncsAuthCheckClient, 0x51);

public:
	QSharedDataPointer<BncsAuthCheckClientData> d;

public:
	quint32 id()
	{
		return id;
	}

	int size()
	{
		return sizeof(d->clientToken) + sizeof(d->exeVersion) + sizeof(d->exeHash) +
		       sizeof(d->numKeys) + sizeof(d->bSpawn) + sizeof(d->keyInfo[0]) +
		       sizeof(d->keyInfo[1]) + (d->exeInfo.size() + 1) + (d->keyOwner.size() + 1);
	}

	void readFrom(QDataStream& ds)
	{
		int bo = ds.byteOrder();
		ds.setByteOrder(QDataStream::LittleEndian);

		ds >> d->clientToken;
		ds >> d->exeVersion;
		ds >> d->exeHash;
		ds >> d->numKeys;
		ds >> d->bSpawn;
		ds >> d->keyInfo[0].length;
		ds >> d->keyInfo[0].product;
		ds >> d->keyInfo[0].publicValue;
		ds >> d->keyInfo[0].unknown;
		ds.readRawData((char*)d->keyInfo[0].hashData, sizeof(d->keyInfo[0].hashData));
		ds >> d->keyInfo[1].length;
		ds >> d->keyInfo[1].product;
		ds >> d->keyInfo[1].publicValue;
		ds >> d->keyInfo[1].unknown;
		ds.readRawData((char*)d->keyInfo[1].hashData, sizeof(d->keyInfo[1].hashData));
		d->exeInfo = readStringFrom(ds);
		d->keyOwner = readStringFrom(ds);

		ds.setByteOrder((QDataStream::ByteOrder)bo);
	}

	void writeTo(QDataStream& ds)
	{
		int bo = ds.byteOrder();
		ds.setByteOrder(QDataStream::LittleEndian);

		ds << d->clientToken;
		ds << d->exeVersion;
		ds << d->exeHash;
		ds << d->numKeys;
		ds << d->bSpawn;
		ds << d->keyInfo[0].length;
		ds << d->keyInfo[0].product;
		ds << d->keyInfo[0].publicValue;
		ds << d->keyInfo[0].unknown;
		ds.writeRawData((char*)d->keyInfo[0].hashData, sizeof(d->keyInfo[0].hashData));
		ds << d->keyInfo[1].length;
		ds << d->keyInfo[1].product;
		ds << d->keyInfo[1].publicValue;
		ds << d->keyInfo[1].unknown;
		ds.writeRawData((char*)d->keyInfo[1].hashData, sizeof(d->keyInfo[1].hashData));
		writeStringTo(ds, d->exeInfo);
		writeStringTo(ds, d->keyOwner);

		ds.setByteOrder((QDataStream::ByteOrder)bo);
	}
};

#endif /* BNCSAUTHCHECKCLIENT_H_ */
